var t = getApp();

Page({
    data: {
        active: 0,
        list: [],
        mallModel: [],
        numModel: [],
        num: 0,
        isNoData: !1
    },
    onLoad: function(t) {
        this.getData();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    getData: function() {
        this.into();
        var o = this, a = t.getNetAddresss("coupon.exchange-center.index");
        t._getNetWork({
            url: a,
            data: {
                platform_id: 0
            },
            success: function(a) {
                var n = a.data;
                1 == n.result ? (o.setData({
                    list: n.data.list
                }), t._isTextEmpty(n.data.list) && o.setData({
                    isNoData: !0
                })) : wx.showToast({
                    title: n.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    into: function() {
        this.setData({
            list: [],
            mallModel: [],
            numModel: [],
            num: 0,
            isNoData: !1
        });
    },
    numberLeft: function(t) {
        var o = t.target.dataset.index, a = this.data.numModel, n = this.data.mallModel;
        this.data.numModel[o] ? (a[o] = a[o] - 1, 0 == a[o] ? n.forEach(function(o, a) {
            if (o.coupon_id == t.target.dataset.info.coupon_id) return n.splice(a, 1);
        }) : n.forEach(function(o, a) {
            if (o.coupon_id == t.target.dataset.info.coupon_id) return n[a].total = parseInt(n[a].total) - 1;
        }), this.setData({
            numModel: a,
            mallModel: n,
            num: this.data.num - 1
        })) : console.log("error");
    },
    numberRight: function(t) {
        console.log(t);
        var o = t.target.dataset.info.belongs_to_coupon.goods_ids[0], a = {
            coupon_id: t.target.dataset.info.coupon_id,
            goods_id: o.has_many_options && o.has_many_options.length > 0 ? o.id : o,
            option_id: o.has_many_options && o.has_many_options.length > 0 ? o.has_many_options[0].id : ""
        }, n = t.target.dataset.index, e = this.data.numModel, i = this.data.mallModel;
        if (this.data.numModel[n]) {
            if (e[n] + 1 > t.target.dataset.info.total) return void wx.showToast({
                title: "该券数量不足",
                icon: "none",
                duration: 1e3
            });
            e[n] = e[n] + 1, i.forEach(function(o, a) {
                if (o.coupon_id == t.target.dataset.info.coupon_id) return i[a].total = parseInt(i[a].total) + 1;
            }), this.setData({
                numModel: e,
                mallModel: i,
                num: this.data.num + 1
            });
        } else e[n] = 1, a.total = 1, i.push(a), this.setData({
            numModel: e,
            mallModel: i,
            num: this.data.num + 1
        });
    },
    gotoExchange: function() {
        if (0 != this.data.mallModel.length) {
            var t = [];
            this.data.mallModel.forEach(function(o) {
                t.push(o.goods_id);
            }), t = t.join(",");
            var o = JSON.stringify(this.data.mallModel);
            console.log(this.data.mallModel, t), wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=fromExchange&goodsId=" + t + "&exchangeData=" + o
            });
        } else wx.showToast({
            title: "请选择兑换券",
            icon: "none",
            duration: 1e3
        });
    }
});